<?php

namespace App\Http\Controllers\Api\User;

use Illuminate\Http\Request;
use App\Http\Requests\Api\ApiRequest;
use App\Http\Controllers\Controller;
use App\Repos\InviteRepo;
use App\Repos\UserRepo;

class InviteController extends Controller
{
   // 别人邀约我的
   public function index(ApiRequest $request)
   {
   		$rs['status'] = true;
   		$where['to_user_id'] = $request->user_id;
         $where['i.status']   = 1;
         if($request->status) {
            $where['i.status'] = $request->status;
         } 
         $orderby = 'id';
         if($request->orderby) {
            $order_arr = ['id','face_score','point'];
            $orderby = trim($request->orderby);
            if( !in_array($orderby,$order_arr) ) $orderby = 'id';
         }
   		$list = InviteRepo::getToMyPages($where,10,$orderby);
   		$rs['data'] = $list;
   		return response()->json($rs);
   }

   // 我要约别人
   public function fromMy(ApiRequest $request)
   {
         $rs['status'] = true;
         $where['i.from_user_id'] = $request->user_id;
         if($request->status) {
            $where['i.status']       = $request->status;   
         }          
         $list = InviteRepo::getPages($where,2);
         $rs['data'] = $list;
         return response()->json($rs);
   }

   // // 查看邀请人的信息（别人约我，我查看别人的信息）
   // public function inviterInfo(ApiRequest $request)
   // {
   // 		$rs['status'] = false;
   // 		$where['from_user_id'] = $request->from_id;
   // 		$where['to_user_id']   = $request->user_id;
   // 		$flag = InviteRepo::find($where);
   // 		if($flag) {
   // 			$rs['status'] = true;
   // 			$rs['data'] = UserRepo::find(['id'=>$request->from_id]);
   // 			return response()->json($rs);
   // 		}
   // 		return response()->json($rs);
   // 	}

   /**
    * 反馈别人的邀约
    */
   public function feedBcak(ApiRequest $request)
   {
      $rs['status'] = false;
      $where['id']    = (int)$request->invite_id;
      $data['status'] = (int)$request->status;
      $data['is_read'] = 1;
      $flag = InviteRepo::update($where,$data);
      if($flag) {
         $rs['status'] = true;
      }
      return response()->json($rs);
   }
}
